<template>
	<a-row :gutter="10">
		<a-col :xs="24" :sm="24" :md="24" :lg="6" :xl="6">
			<a-form ref="dictSearchFormRef" name="dict_search" :model="searchFormState">
				<a-row :gutter="24">
					<a-col :span="12">
						<a-form-item name="searchKey">
							<a-input v-model:value="dictSearchFormState.dictName" placeholder="字典类型" />
						</a-form-item>
					</a-col>
					<a-col :span="12">
						<a-button type="primary" @click="loadTreeData()">
							<template #icon><SearchOutlined /></template>
						</a-button>
						<a-button class="leisure-buttom-left" @click="dictFormRef.onOpen('N')">
							<template #icon><plus-outlined  /></template>
						</a-button>
						<a-button v-if="showDictDelete" type="danger" class="leisure-buttom-left" @click="dictFormRef.onOpen()">
							<template #icon><delete-outlined  /></template>
						</a-button>
					</a-col>
				</a-row>
			</a-form>
			<a-tree
				v-if="treeData.length > 0"
				v-model:expandedKeys="defaultExpandedKeys"
				:tree-data="treeData"
				:field-names="treeFieldNames"
				@select="treeSelect"
			>
			</a-tree>
			<a-empty v-else :image="Empty.PRESENTED_IMAGE_SIMPLE" />
		</a-col>
		<a-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18">
			<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item name="searchKey" label="字典标签">
							<a-input v-model:value="searchFormState.searchKey" placeholder="请输入字典标签" />
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-button type="primary" @click="tableRef.refresh(true)">
							<template #icon><SearchOutlined /></template>
							查询
						</a-button>
						<a-button class="snowy-buttom-left" @click="reset">
							<template #icon><redo-outlined /></template>
							重置
						</a-button>
					</a-col>
				</a-row>
			</a-form>
			<a-divider class="m-3 mx-0" />
			<s-table
				ref="tableRef"
				:columns="columns"
				:data="loadData"
				:expand-row-by-click="true"
				bordered
				:tool-config="toolConfig"
				:row-key="(record) => record.id"
			>
				<template #operator class="table-operator">
					<a-button type="primary" @click="dictDataFormRef.onOpen(undefined, '1', searchFormState.dictCode)">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
				</template>
				<template #bodyCell="{ record }">
					<template>
						<a @click="dictFormRef.onOpen(record, 1)">编辑</a>
					</template>
				</template>
			</s-table>
		</a-col>
	</a-row>
	<Form ref="dictFormRef" @successful="formSuccessful()" />
	<DictDataForm ref="dictDataFormRef" @successful="formSuccessful()" />
</template>

<script setup>
	import { Empty } from 'ant-design-vue'
	import DictApi from '@/api/set/DictApi'
	import DictDataApi from '@/api/set/DictDataApi'
	import Form from './DictForm.vue'
	import DictDataForm from './DictDataForm.vue'
	import tool from '@/utils/tool'
	const searchFormState = ref({})
	const dictSearchFormState = ref({})
	const columns = [
		{
			title: '字典标签',
			dataIndex: 'label',
			width: 150
		},
		{
			title: '字典值',
			dataIndex: 'value',
			width: 300
		},
		{
			title: '字典类型',
			dataIndex: 'dictCode',
			width: 150
		},
		{
			title: '排序',
			dataIndex: 'sort',
			width: 150
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '200px'
		}
	]
	// 定义tableDOM
	const tableRef = ref(null)
	const dictFormRef = ref()
	const dictSearchFormRef = ref()
	const dictDataFormRef = ref()
	const searchFormRef = ref()
	// 默认展开的节点
	const defaultExpandedKeys = ref([])
	const treeData = ref([])
	// 替换treeNode 中 title,key,children
	const treeFieldNames = { children: 'children', title: 'dictName', key: 'dictCode' }
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }

	// 表格查询 返回 Promise 对象
	const loadData = (parameter) => {
		loadTreeData()
		parameter.isSystem = 'N'
		return DictDataApi.dictDataPage(Object.assign(parameter, searchFormState.value)).then((data) => {
			return data
		})
		// return DictDataApi.dictDataPage(Object.assign(parameter, searchFormState.value)).then((data) => {

		// 	if (data.records) {
		// 		if (searchFormState.value.dictCode) {
		// 			let dataArray = []
		// 			data.records.forEach((item) => {
		// 				const obj = data.records.find((f) => f.id === item.dictCode)
		// 				if (!obj) {
		// 					dataArray.push(item)
		// 				}
		// 			})
		// 			if (dataArray.length === 1) {
		// 				data.records.forEach((item) => {
		// 					if (item.id === dataArray[0].id) {
		// 						item.level = '上级'
		// 					}
		// 				})
		// 			}
		// 			dataArray = []
		// 		}
		// 	}
		// 	return data
		// })
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 加载左侧的树
	const loadTreeData = () => {
		const param = {
			isSystem: 'N'
		}
		DictApi.dictList(param).then((res) => {
			if (res) {
				treeData.value = res
			}
		})
	}
	// 点击树查询
	const treeSelect = (selectedKeys) => {
		if (selectedKeys && selectedKeys.length > 0) {
			searchFormState.value.dictCode = selectedKeys.toString()
			if (!columns.find((f) => f.title === '层级')) {
				columns.splice(2, 0, {
					title: '层级',
					dataIndex: 'level',
					width: 100
				})
			}
		} else {
			delete searchFormState.value.dictCode
			columns.splice(2, 1)
		}
		tableRef.value.refresh(true)
	}
	// 表单界面回调
	const formSuccessful = () => {
		tableRef.value.refresh()
		refreshStoreDict()
	}
	// 刷新store中的字典
	const refreshStoreDict = () => {
		DictApi.dictList().then((res) => {
			tool.data.set('DICT_DATA', res)
		})
	}
</script>

<style scoped>
	.ant-form-item {
		margin-bottom: 0 !important;
	}
	.leisure-buttom-left {
		margin-left: 4px;
	}
</style>
